package com.say.activity;

import android.app.Activity;
import android.app.DownloadManager;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.say.test.R;
import com.say.tool.Tool;
/*
 * 使用android自带下载管理下载。
 */
public class DownloadActivity extends Activity {
	/** Called when the activity is first created. */

	private EditText et;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Tool.logInfo("" + DownloadActivity.class.getSimpleName() + "onCreate()");
		setContentView(R.layout.activity_download);

		et = (EditText) findViewById(R.id.editText1);
	}

	public void download(View v) {
		if (et == null || et.getText() == null || et.getText().length()==0) {
			Toast.makeText(this, "URL错误。", Toast.LENGTH_SHORT).show();
			return;
		}
		Toast.makeText(this, "开始下载。。。", Toast.LENGTH_SHORT).show();
		String apkUrl = et.getText().toString();
		
		DownloadManager downloadManager = (DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);
		DownloadManager.Request request = new DownloadManager.Request(Uri.parse(apkUrl));
		request.setDestinationInExternalPublicDir("sssay", "MeiLiShuo.apk");
		
		 request.setTitle("MeiLiShuo");
		 request.setDescription("MeiLiShuo desc");
		 request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
//		 request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
//		 request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
		 request.setMimeType("application/vnd.android.package-archive");
		 
		 
		long downloadId = downloadManager.enqueue(request);
		Toast.makeText(this, "downloadId:"+downloadId, Toast.LENGTH_LONG).show();
	}

	public void onResume() {
		Tool.logInfo("" + DownloadActivity.class.getSimpleName() + "OnResume()");
		super.onResume();
	}

	public void onPause() {
		Tool.logInfo("" + DownloadActivity.class.getSimpleName() + "onPause()");
		super.onPause();
	}
}